/**
 * @Title: .java
 * @Package com.sino.keydome
 * @Description: TODO(用一句话描述该文件做�?�?)
 * @author hpq
 * @date 2015-10-28 下午3:27:54
 */
	
package com.sinoglobal.wallet.widget;

import com.sinoglobal.wallet.R;
import com.sinoglobal.wallet.widget.KeyboardUtil.FinishListener;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.InputFilter;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
//import android.view.MotionEvent;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author hpq
 * @createdate 2015-10-28 下午3:27:54
 * @Description: TODO(自定义密码键盘并显示密码布局)
 */
public class WGridPasswordView extends LinearLayout {
    private static final int DEFAULT_PASSWORDLENGTH = 6;
    private static final String DEFAULT_TRANSFORMATION = "●";
//	private static final String TAG = GridPasswordView.class.getSimpleName();
	private boolean IsLibrary = false;//当Activity作为jar包使用是该值为true 否则为false
    private TextView[] viewArr;
	private EditText edit;
	private PasswordListener mPasswordListener;
//	private Context context;
	protected String mPassword = "";
	
	public interface PasswordListener{
		void setPassWord(String pwd);
	}

	public WGridPasswordView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	public WGridPasswordView(Context context) {
		super(context);
		initView(context);
	}

	@SuppressLint("NewApi") private void initView(Context context) {
//		this.context = context;
		viewArr = new TextView[DEFAULT_PASSWORDLENGTH];
		Drawable tvDrawable;
		if(IsLibrary){
        	tvDrawable = getResources().getDrawable(R.drawable.w_pwd_right_border);
			LayoutInflater.from(context).inflate(R.layout.wallet_pwd_view, this);
			edit = (EditText)findViewById(R.id.edit);
		}else{
			tvDrawable = getResources().getDrawable(R.drawable.w_pwd_right_border);
			LayoutInflater.from(context).inflate(R.layout.wallet_pwd_view, this);
			edit = (EditText)findViewById(R.id.edit);
		}
		edit.setInputType(InputType.TYPE_NULL);
		edit.setFilters(new InputFilter[]{
				new InputFilter.LengthFilter(DEFAULT_PASSWORDLENGTH)});
		int index = 0;
        while (index < DEFAULT_PASSWORDLENGTH) {
            TextView textView = new TextView(context);
            int sdk = android.os.Build.VERSION.SDK_INT;
            if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
            	textView.setBackgroundDrawable(tvDrawable);
            } else {
            	textView.setBackground(tvDrawable);
            }
		
			textView.setGravity(Gravity.CENTER);
            LayoutParams textViewParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1f);
            addView(textView, textViewParams);
            viewArr[index] = textView;
            index++;
        }       
	}

//	private int parseResLoc(String resStr) {
//		String[] split = resStr.split("\\.");
//		int resId = -1;
//		if(split.length==3){
//			resId = MResource.getIdByName(context, split[1], split[2]);
//		}
//		return resId;
//	}

//	@SuppressLint("ClickableViewAccessibility") @Override
//	public boolean onTouchEvent(MotionEvent event) {
//		Log.d(TAG, "onTouchEvent :"+event.getAction());
//		if(keyUtil!=null)
//			keyUtil.showKeyboard();
//		return super.onTouchEvent(event);
//	}

	public void setKeyUtil(final KeyboardUtil keyUtil) {
		keyUtil.setFinishListener(new FinishListener() {

			@Override
			public void onInputChar(int index) {
				if(index < DEFAULT_PASSWORDLENGTH)
					viewArr[index].setText(DEFAULT_TRANSFORMATION);
			}
			
			@Override
			public void onFinished(String inputStr) {
				mPassword = inputStr;
				if(mPasswordListener!=null)
					mPasswordListener.setPassWord(inputStr);
			/*	if(inputStr.length()==DEFAULT_PASSWORDLENGTH)
					keyUtil.hideKeyboard();*/
			}
			
			@Override
			public void deleteChar(int index) {
				if(index < DEFAULT_PASSWORDLENGTH){
					viewArr[index].setText("");
				}
			}
		});
	}
	
	public void setPasswordListener(PasswordListener l){
		mPasswordListener = l;
	}

	public EditText getEditText() {
//		edit.setFocusable(true);
//		edit.setFocusableInTouchMode(true);
//		edit.requestFocus();
		return edit;
	}

	public String getPassWord(){
		return mPassword;
	}
	
	public void resetPassword(){
		for(int i=0; i<mPassword.length(); i++){
			viewArr[i].setText("");
		}
		mPassword = "";
		edit.setText("");
	}
}
